package com.chocho.producerserver.controller;

import com.chocho.producerserver.bean.User;
import com.chocho.producerserver.service.UserService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.VUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * Created by chocho on 2022/3/20.
 */
@CrossOrigin
@RestController
public class LoginController {
    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public Msg verifyLogin(@RequestBody User user){
        return userService.login(user);
    }

    @GetMapping("/getMessage")
    public Msg getMessage(){
        return Msg.success().add("msg", "你已通过验证");
    }

    @PostMapping("/register")
    @ResponseBody
    public Msg register(@RequestBody VUser user){
        logger.info(user.getUserName() + ":" + user.getUserPwd());
        if(userService.selectUserByUsername(user.getUserName()) != null){
            return Msg.fail().add("msg", "用户名已存在！");
        }
        return userService.register(user) ?  Msg.success() : Msg.fail();
    }

    @PreAuthorize("hasAuthority('user:logout')")
    @GetMapping("/logout")
    @ResponseBody
    public Msg logout(){
        logger.info("================ssyusgygty");
        return userService.logout() ? Msg.success() : Msg.fail();
    }
}
